/**
 * Created by Brook on 2020/4/9.
 */

(function () {
    var async = require('async');
    var sql_config = require('../../serverconfig.js');
    var model_sql = require('../../tool/db_helper.js').init(sql_config.Model_Config);
    var SQL_TXT_API = sql_config.SQL_TXT_API;


    var messageData = {
        "message": "请求成功",
        "data": {
            "code": 200,
            "data": [],
            "message": ""
        }
    };


    //获取列表
    exports.post_getList = function(req, res) {
        async.auto({
            check_pars:function (cb){
                res.header('Access-Control-Allow-Origin', '*');
                cb(null, req.body);
            },
            get_data:['check_pars', function (cb, result) {
                var pars = result.check_pars;
                var SQL_TXT = '';
                var setdata = '';
                if(JSON.stringify(pars)=='{}') {
                    SQL_TXT = SQL_TXT_API.get_list;
                } else {
                    for (var i in pars){
                        setdata += i + " = '" + pars[i] + "' AND "
                    };
                    setdata = setdata.substr(0,setdata.length-5);
                    SQL_TXT = SQL_TXT_API.get_list_condition + setdata + ' ORDER BY createtime ASC;';
                };
                model_sql.query(SQL_TXT,function(db_err,db_result){
                    db_err?cb(db_err):cb(null,db_result);
                });
            }],
            create_map:['get_data', function (cb, result) {
                var map = {};
                async.forEach(result.get_data,function(each_item,each_callback){
                    map[each_item.id]=each_item;
                    map[each_item.id]['modelid']='';
                    each_callback();
                },function(each_err){
                    each_err?cb(each_err):cb(null, map);
                });
            }],
            get_listID:['create_map', function (cb, result) {
                var liisId = [];
                async.forEach(result.get_data,function(each_item,each_callback){
                    liisId.push(each_item.id);
                    each_callback();
                },function(each_err){
                    each_err?cb(each_err):cb(null, liisId);
                });
            }],
            get_modelId:['get_listID', function (cb, result) {
                if (result.get_listID.length == 0) {
                    cb(null, [])
                } else {
                    model_sql.query(SQL_TXT_API.get_modelId_list,[result.get_listID],function(db_err,db_result){
                        db_err?cb(db_err):cb(null,db_result);
                    });
                };
            }],
            merge_data:['get_modelId', function (cb, result) {
                if (result.get_listID.length == 0) {
                    cb(null, [])
                } else {
                    var map = result.create_map;
                    async.forEach(result.get_modelId,function(each_item,each_callback){
                        if (map[each_item.listid]) {
                            map[each_item.listid]['modelid'] = each_item.modelid;
                        };
                        each_callback();
                    },function(each_err){
                        each_err?cb(each_err):cb(null, map);
                    });
                }

            }],
            //回传数据
            response_data:['merge_data', function (cb, result) {
                messageData.data.data = result.merge_data;
                res.json(messageData);
                cb(null, [])
            }]
        },function(err,results){
            if(err){
                console.log(err);
                res.json(r);
            }else{
                var r = {code:0,message:results.get_pars};
                //console.log(r);
            };
        });
    };
    //查询modelId
    exports.post_modelID = function(req, res) {
        async.auto({
            check_pars:function (cb){
                res.header('Access-Control-Allow-Origin', '*');
                cb(null, req.body);
            },
            get_listid:['check_pars', function (cb, result) {
                model_sql.query(SQL_TXT_API.get_modelId, [result.check_pars.modeId],function(db_err,db_result){
                    db_err?cb(db_err):cb(null,db_result);
                });
            }],
            check_data:['get_listid', function (cb, result) {
                if (result.get_listid.length == 0) {
                    messageData.data.code = 201;
                    messageData.data.message = "无效的ModelId";
                    res.json(messageData);
                } else {
                    var SQL_TXT_API = SQL_TXT_API.get_list_condition + 'id='+result.get_listid[0].listid +';';
                    model_sql.query(SQL_TXT_API,function(db_err,db_result){
                        db_err?cb(db_err):cb(null,db_result);
                    });
                };
            }],
            merge_data:['check_data', function (cb, result) {
                if (result.check_data.length == 0) {
                    messageData.data.code = 202;
                    messageData.data.message = "未查询到解析任务";
                    res.json(messageData);
                } else {
                    result.check_data[0]['modelid'] = result.check_pars.modeId;
                    cb(null, result.check_data);
                };
            }],
            //回传数据
            response_data:['merge_data', function (cb, result) {
                messageData.data.data = result.merge_data;
                res.json(messageData);
                cb(null, [])
            }]
        },function(err,results){
            if(err){
                console.log(err);
                res.json(r);
            }else{
                var r = {code:0,message:results.get_pars};
                //console.log(r);
            };
        });
    };
    //查询状态日志
    exports.post_stateLog = function(req, res) {
        async.auto({
            check_pars:function (cb){
                res.header('Access-Control-Allow-Origin', '*');
                cb(null, req.body);
            },
            get_logs:['check_pars', function (cb, result) {
                model_sql.query(SQL_TXT_API.get_stateLog, [result.check_pars.uuid],function(db_err,db_result){
                    db_err?cb(db_err):cb(null,db_result);
                });
            }],
            //回传数据
            response_data:['get_logs', function (cb, result) {
                messageData.data.data = result.get_logs;
                res.json(messageData);
                cb(null, [])
            }]
        },function(err,results){
            if(err){
                console.log(err);
                res.json(r);
            }else{
                var r = {code:0,message:results.get_pars};
                //console.log(r);
            };
        });
    };
    //查询客户端信息
    exports.post_clientInfo = function(req, res) {
        async.auto({
            check_pars:function (cb){
                res.header('Access-Control-Allow-Origin', '*');
                cb(null, req.body);
            },
            get_clientInfo:['check_pars', function (cb, result) {
                model_sql.query(SQL_TXT_API.get_clientInfo, [result.check_pars.appid],function(db_err,db_result){
                    db_err?cb(db_err):cb(null,db_result);
                });
            }],
            //回传数据
            response_data:['get_clientInfo', function (cb, result) {
                messageData.data.data = result.get_clientInfo;
                res.json(messageData);
                cb(null, [])
            }]
        },function(err,results){
            if(err){
                console.log(err);
                res.json(r);
            }else{
                var r = {code:0,message:results.get_pars};
                //console.log(r);
            };
        });
    };
    //查询附加属性
    exports.post_attributes = function(req, res) {
        async.auto({
            check_pars:function (cb){
                res.header('Access-Control-Allow-Origin', '*');
                cb(null, req.body);
            },
            get_list_id:['check_pars', function (cb, result) {
                model_sql.query(SQL_TXT_API.get_list_id, [result.check_pars.uuid],function(db_err,db_result){
                    db_err?cb(db_err):cb(null,db_result);
                });
            }],
            get_attributes:['get_list_id', function (cb, result) {
                if (result.get_list_id.length == 0) {
                    messageData.data.data = [];
                    res.json(messageData);
                } else {
                    model_sql.query(SQL_TXT_API.get_attributes, [result.get_list_id[0].id],function(db_err,db_result){
                        db_err?cb(db_err):cb(null,db_result);
                    });
                }
            }],
            //回传数据
            response_data:['get_attributes', function (cb, result) {
                messageData.data.data = result.get_attributes;
                res.json(messageData);
                cb(null, [])
            }]
        },function(err,results){
            if(err){
                console.log(err);
                res.json(r);
            }else{
                var r = {code:0,message:results.get_pars};
                //console.log(r);
            };
        });
    };
    //查询列队数
    exports.post_getQueueNum = function(req, res) {
        async.auto({
            check_pars:function (cb){
                res.header('Access-Control-Allow-Origin', '*');
                cb(null, req.body);
            },
            get_list_id:['check_pars', function (cb, result) {
                model_sql.query(SQL_TXT_API.get_list_id, [result.check_pars.uuid],function(db_err,db_result){
                    db_err?cb(db_err):cb(null,db_result);
                });
            }],
            get_queuenum:['get_list_id', function (cb, result) {
                if (result.get_list_id.length == 0) {
                    messageData.data.code = 201;
                    messageData.data.message = "无效的uuid";
                    res.json(messageData);
                } else {
                    model_sql.query(SQL_TXT_API.get_listNum, [result.get_list_id[0].createtime],function(db_err,db_result){
                        db_err?cb(db_err):cb(null,db_result);
                    });
                };
            }],
            //回传数据
            response_data:['get_queuenum', function (cb, result) {
                messageData.data.data = result.get_queuenum;
                res.json(messageData);
                cb(null, [])
            }]
        },function(err,results){
            if(err){
                console.log(err);
                res.json(r);
            }else{
                var r = {code:0,message:results.get_pars};
                //console.log(r);
            };
        });
    };


}).call(this);
